<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="班组名称" clearable></el-input>
      </el-form-item>
      <el-date-picker
        v-model="value2"
        value-format="yyyy-MM-dd"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-form-item>
        <el-button @click="resetSelectForm">重置</el-button>
        <el-button @click="getTime()">查询</el-button>
<!--        <el-button @click="getDataList()">查询</el-button>-->
<!--        <el-button v-if="isAuth('groupunnormal:groupunnormal:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
<!--        <el-button v-if="isAuth('groupunnormal:groupunnormal:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
        <el-button @click="submit" :disabled="disabled">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="groupid"
        header-align="center"
        align="center"
        label="班组编号">
      </el-table-column>
      <el-table-column
        prop="groupname"
        header-align="center"
        align="center"
        label="班组名称">
      </el-table-column>
      <el-table-column
        prop="curunnormal"
        header-align="center"
        align="center"
        label="电流超规范次数">
      </el-table-column>
      <el-table-column
        prop="volunnormal"
        header-align="center"
        align="center"
        label="电压超规范次数">
      </el-table-column>
      <el-table-column
        prop="pgmunnormal"
        header-align="center"
        align="center"
        label="焊丝超规范次数">
      </el-table-column>
      <el-table-column
        prop="remunnormal"
        header-align="center"
        align="center"
        label="气体超规范次数">
      </el-table-column>
      <el-table-column
        prop="allunnormal"
        header-align="center"
        align="center"
        label="总超规范次数">
      </el-table-column>
      <el-table-column
        prop="allunnormalemp"
        header-align="center"
        align="center"
        label="总超规范人数">
      </el-table-column>
      <el-table-column
        prop="grouptime"
        header-align="center"
        align="center"
        label="时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.groupid)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.groupid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './groupunnormal-add-or-update'
  import xlsx from 'xlsx'
  import {Loading} from 'element-ui'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        disabled: false,
        loading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        value1: '',
        value2: ''
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
      // this.getTime()
    },
    methods: {
      // 测试获取到的时间
      getTime(){
        //console.log(this.value2)
        console.log(this.dataForm.key)
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/groupunnormal/groupunnormal/getshuju'),
          method: 'get',
          params: this.$http.adornParams({
            'key': this.dataForm.key,
            'startTime': this.value2[0],
            'endTime': this.value2[1]
          })
        }).then(({data}) => {
          if (data) {
            console.log(data)
            this.dataList = data
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      // 重置按钮
      resetSelectForm(){
        this.dataForm.key = '';
        this.value2 = '';
        this.getTime();
      },
      // 导出操作
      submit(){
        if(this.dataListSelections.length <= 0){
          this.$message({
            message: "请选择需要导出的数据!",
            type: 'warning',
            showClose: true
          });
          return;
        }
        this.disabled = true;
        let loadingInstance = Loading.service({
          text: "正在导出...",
          background: "rgba(0,0,0,.5)"
        });
        let arr = this.dataListSelections.map(item => {
          console.log(item)
          return {
            班组编号: item.groupid,
            班组名称: item.groupname,
            电流超规范次数: item.curunnormal,
            电压超规范次数: item.volunnormal,
            焊丝超规范次数: item.pgmunnormal,
            气体超规范次数: item.remunnormal,
            总超规范次数: item.allunnormal,
            总超规范人数: item.allunnormalemp,
            时间: item.grouptime
          };
        });
        //console.log(arr)
        let sheet = xlsx.utils.json_to_sheet(arr);
        const book1 = xlsx.utils.book_new();
        xlsx.utils.book_append_sheet(book1,sheet,"sheet1");
        xlsx.writeFile(book1,`user${new Date().getTime()}.xls`);
        loadingInstance.close();
        this.disabled = false;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/groupunnormal/groupunnormal/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.groupid
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/groupunnormal/groupunnormal/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
